package com;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.SystemClock;
import android.os.signway.SignwayManager;
import android.util.Log;
import android.view.Choreographer;

import com.signway.swmediaplayerdemo.FpsMonitor;

public class SubtitleTest {
    public static final String TAG = "SubtitleTest";

    private SignwayManager sm;

    private Bitmap bitmap;
    private static final String subtitleH  = "横向滚动测试字幕，横向滚动测试字幕，横向滚动测试字幕，横向滚动测试字幕，横向滚动测试字幕，横向滚动测试字幕，横向滚动测试字幕，横向滚动测试字幕。";
    private static final String subtitleHR = "。幕字试测动滚向横，幕字试测动滚向横，幕字试测动滚向横，幕字试测动滚向横，幕字试测动滚向横，幕字试测动滚向横，幕字试测动滚向横，幕字试测动滚向横";
    private static final String subtitleV  = "纵向滚动测试字幕，纵向滚动测试字幕，纵向滚动测试字幕，纵向滚动测试字幕，纵向滚动测试字幕，纵向滚动测试字幕，纵向滚动测试字幕，纵向滚动测试字幕。";
    private static final String subtitleVR = "。幕字试测动滚向纵，幕字试测动滚向纵，幕字试测动滚向纵，幕字试测动滚向纵，幕字试测动滚向纵，幕字试测动滚向纵，幕字试测动滚向纵，幕字试测动滚向纵";

    public void init(SignwayManager sm, int width, int height) {
        this.sm = sm;
        initSubtitle(width, height);
    }

    public void release() {
        onDraw = false;
        Choreographer.getInstance().removeFrameCallback(frameCallback);
        sm.subtitleRelease();
    }

    private static boolean onDraw = false;
    private void initSubtitle(int width, int height) {
        Log.d(TAG, "width " + width + ", height " + height);
        bitmap = createSubtitle(subtitleH);
        sm.subtitleInit(0, 1000, width, bitmap.getHeight(), bitmap, 2, 4);
        sm.subtitlePrepare();
        sm.subtitleFlush();
        onDraw = true;
        Choreographer.getInstance().postFrameCallback(frameCallback);
    }

    private long lastFrameTimeNanos;
    private Choreographer.FrameCallback frameCallback = new Choreographer.FrameCallback() {
        @Override
        public void doFrame(long frameTimeNanos) {
            if (lastFrameTimeNanos > 0 && (frameTimeNanos - lastFrameTimeNanos > 17000000)) {
                //Log.d(TAG, "Interval since last VSync: " + (frameTimeNanos - lastFrameTimeNanos) + " ns");
            }
            lastFrameTimeNanos = frameTimeNanos;

            long startMs = SystemClock.elapsedRealtime();
            if (onDraw) {
                sm.subtitleFlush();
            }
            if (SystemClock.elapsedRealtime() - startMs > 2) {
                //Log.d(TAG, "subtitleFlush time " + (SystemClock.elapsedRealtime() - startMs));
            }
            Choreographer.getInstance().postFrameCallback(this);

            FpsMonitor.get().updateFPS(frameTimeNanos);
        }
    };

    private Bitmap createSubtitle(String text) {
        int textWidth;
        int textHeight = 96;
        Bitmap bitmap;

        Paint bgRect=new Paint();
        bgRect.setStyle(Paint.Style.FILL);
        bgRect.setColor(Color.GRAY);

        Paint textPaint = new Paint();
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setStrokeWidth(8);
        textPaint.setTextSize(72);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.BLACK);
        textWidth = (int) textPaint.measureText(text);
        bitmap = Bitmap.createBitmap(textWidth + 1, textHeight, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);

        //计算baseline
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
        float baseline = textHeight / 2 + distance;
        c.drawText(text, textWidth / 2, baseline, textPaint);

        bgRect.setColor(Color.RED);
        c.drawLine(0, 0, bitmap.getWidth(), 0, bgRect);
        c.drawLine(0, 0, 0, bitmap.getHeight(), bgRect);
        c.drawLine(bitmap.getWidth() - 1, 0, bitmap.getWidth(), bitmap.getHeight(), bgRect);
        c.drawLine(0,  bitmap.getHeight() - 1, bitmap.getWidth(), bitmap.getHeight(), bgRect);

        return bitmap;
    }
}
